home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gmp-132.lha / gmp-1.3.2 / mpq_cmp.c < prev    next >
C/C++ Source or Header  |  1993-05-19  |  2KB  |  77 lines

  1. /* mpq_cmp(u,v) -- Compare U, V.  Return positive, zero, or negative
  2.    based on if U > V, U == V, or U < V.
  3.  
  4. Copyright (C) 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of the GNU MP Library.
  7.  
  8. The GNU MP Library is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. The GNU MP Library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with the GNU MP Library; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "gmp.h"
  23. #include "gmp-impl.h"
  24.  
  25. int
  26. #ifdef __STDC__
  27. mpq_cmp (const MP_RAT *op1, const MP_RAT *op2)
  28. #else
  29. mpq_cmp (op1, op2)
  30.      const MP_RAT *op1;
  31.      const MP_RAT *op2;
  32. #endif
  33. {
  34.   mp_size num1_size = op1->num.size;
  35.   mp_size den1_size = op1->den.size;
  36.   mp_size num2_size = op2->num.size;
  37.   mp_size den2_size = op2->den.size;
  38.   mp_size tmp1_size, tmp2_size;
  39.   mp_ptr tmp1_ptr, tmp2_ptr;
  40.   mp_size num1_sign;
  41.   int cc;
  42.  
  43.   if (num1_size == 0)
  44.     return -num2_size;
  45.   if (num2_size == 0)
  46.     return num1_size;
  47.   if ((num1_size ^ num2_size) < 0) /* I.e. are the signs different? */
  48.     return num1_size;
  49.  
  50.   num1_sign = num1_size;
  51.   num1_size = ABS (num1_size);
  52.   num2_size = ABS (num2_size);
  53.  
  54.   tmp1_size = num1_size + den2_size;
  55.   tmp2_size = num2_size + den1_size;
  56.  
  57.   if (tmp1_size != tmp2_size)
  58.     return (tmp1_size - tmp2_size) ^ num1_sign;
  59.  
  60.   tmp1_ptr = (mp_ptr) alloca (tmp1_size * BYTES_PER_MP_LIMB);
  61.   tmp2_ptr = (mp_ptr) alloca (tmp2_size * BYTES_PER_MP_LIMB);
  62.  
  63.   tmp1_size = (num1_size >= den2_size)
  64.     ? mpn_mul (tmp1_ptr, op1->num.d, num1_size, op2->den.d, den2_size)
  65.     : mpn_mul (tmp1_ptr, op2->den.d, den2_size, op1->num.d, num1_size);
  66.  
  67.   tmp2_size = (num2_size >= den1_size)
  68.     ? mpn_mul (tmp2_ptr, op2->num.d, num2_size, op1->den.d, den1_size)
  69.     : mpn_mul (tmp2_ptr, op1->den.d, den1_size, op2->num.d, num2_size);
  70.  
  71.   cc = tmp1_size - tmp2_size != 0
  72.     ? tmp1_size - tmp2_size : mpn_cmp (tmp1_ptr, tmp2_ptr, tmp1_size);
  73.  
  74.   alloca (0);
  75.   return (num1_sign < 0) ? -cc : cc;
  76. }
  77.